import { Controller, Get, Query, Req } from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { Public } from '@/decorators/public.decorator'
import { MenuService } from '@/menu/menu.service'
import { generateConstantRoutes, generateUserRoutes } from '@/utils/formatMenu'
import { RouteService } from './route.service'

@Controller('route')
@ApiTags('路由模块')
@ApiBearerAuth()
export class RouteController {
  constructor(
    private readonly menuService: MenuService,
    private readonly routeService: RouteService
  ) {}

  @Public()
  @Get('getConstantRoutes')
  @ApiOperation({ summary: '获取静态路由' })
  async getConstantRoutes() {
    const routes = await this.menuService.findAll()
    return generateConstantRoutes(routes)
  }

  @Get('getUserRoutes')
  @ApiOperation({ summary: '获取当前登录可访问路由' })
  async getUserRoutes(@Req() req: any) {
    const routes = await this.menuService.findAllAsTrees()
    return {
      home: 'home',
      routes: generateUserRoutes(routes, req?.user.roles),
    }
  }

  @Get('isRouteExist')
  @ApiOperation({ summary: '路由是否存在' })
  async isRouteExist(@Query('routeName') routeName: string) {
    const routes = await this.menuService.findOneByRouteName(routeName)
    return routes ? true : false
  }
}
